package com.wms.service.impl;

import com.wms.entity.SysUser;
import com.wms.mapper.SysUserMapper;
import com.wms.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

/**
 * ---Do First---
 *
 * @author:wellseasun
 * @date:2025/4/21 下午 1:06
 * @desc:
 */
@Service
public class SysUserServiceImpl implements SysUserService {
    @Autowired
    SysUserMapper sysUserMapper;

    @Override
    public SysUser login(SysUser sysUser) {
        return sysUserMapper.login(sysUser);
    }

    @Override
    public Boolean update(SysUser sysUser) {
        String filePath = sysUser.getAvatar();
        sysUser.setAvatar(convertImageUrl(filePath));
        return sysUserMapper.update(sysUser);
    }

    @Override
    public Boolean save(SysUser sysUser) {

        return sysUserMapper.save(sysUser);
    }

    // 转换图片地址
    private String convertImageUrl(String filePath) {
        // 分割路径并获取最后两位
        List<String> pathSegments = Arrays.asList(filePath.split("/"));
        int size = pathSegments.size();
        // 安全获取最后两个元素（当元素不足两个时取全部）
        List<String> lastTwoSegments = pathSegments.subList(
                Math.max(size - 2, 0),  // 起始索引不小于0
                size
        );
        // 合并为字符串（可根据需要修改连接符）
        String result = String.join("/", lastTwoSegments);
        return result;
    }
}
